package com.example._03vehicle.controller;

import com.example._03vehicle.base.response.JsonResult;
import com.example._03vehicle.mapper.NoticeMapper;
import com.example._03vehicle.pojo.vo.NoticeSelectVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Api(tags = "03.公告管理模块")
@RequestMapping(value = "/v1/notice/")
@RestController
public class NoticeCtroller {
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;
    @GetMapping(value = "selectNotice/{number}")
    @ApiOperation(value = "查询公告功能")
    @ApiOperationSupport(order=10)
    @ApiImplicitParam(name = "number", value = "公告类型", required = true, dataType = "int")
    public JsonResult selectNotice(@PathVariable int number){
        log.debug("number:"+number);
         List<NoticeSelectVo> list=noticeMapper.selectNotice(number);
         if(list.size()!=0){
             return JsonResult.success(list);
         }else {return JsonResult.fail();}
    }
    @GetMapping("list-redis")
    @ApiOperation(value = "添加redis查询公告功能")
    @ApiOperationSupport(order=20)
    public JsonResult select(){
        ValueOperations<String,Serializable> ops=redisTemplate.opsForValue();
        Serializable notice=ops.get("notice");
        if(notice!=null){
            log.debug("首页数据来自Redis数据库");
            return JsonResult.success(notice);
        }
        //缓存不存在
        log.debug("首页数据来自数据库");
        List<NoticeSelectVo> list=noticeMapper.selectNotice(1);
        ops.set("notice",(Serializable) list ,30, TimeUnit.SECONDS);
        if(list.size()!=0){
            return JsonResult.success(list);
        }else {return JsonResult.fail();}
    }
}
